//
//  ReadViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)NSMutableArray *scrollerArray;  //轮播图照片
@end

@implementation ReadViewController
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        
        layout.itemSize = CGSizeMake(100, 100);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3+64, kWidth, kHeight/2)collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor cyanColor];
        
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (NSMutableArray *)scrollerArray{
    if (!_scrollerArray) {
        _scrollerArray = [[NSMutableArray alloc]init];
    }
    return _scrollerArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    //请求数据
    [RequestManager fetchDataWithUrl:kReadUrl parameter:@{} mehtod:@"GET" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];


        //添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            [self.scrollerArray addObject:smallDic[@"img"]];
        }
        //添加collectionView的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadModel *model = [[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
            
        }
        
    } updata:^{
        //创建轮播图
        LSDScrollView *scrollview = [[LSDScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3-20) imageArray:self.scrollerArray];
        [self.view addSubview:scrollview];
        [self.collectionView reloadData];
        NSLog(@"刷新界面");
    }];
    
    //注册xib
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
    [cell setCellWithModel:self.dataSource[indexPath.row]];
  
    
    return cell;
    
}
//collectionItem点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //通过xib获取控制器
    ReadListViewController *readListVC = [[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];
    
    //传值
    ReadModel *model = self.dataSource[indexPath.row];
    readListVC.typeID = model.type;
    
   
    [self.navigationController pushViewController:readListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
